/*
 * FrameSchulzeHandler.java
 *
 * Copyright (C) 2014 Fabian Wenzelmann
 *
 * This file is part of stura_voting.
 *
 * stura_voting is free software: you
 * can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * stura_voting is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with stura_voting.
 *
 * If not, see <http://www.gnu.org/licenses/>.
 */

package de.stura.schulze;

import javax.swing.JFrame;

import de.stura.InfoFrame;

public class FrameSchulzeHandler implements SchulzeWarningHandler {

    private StringBuilder htmlText;
    private boolean hasMod;
    
    public FrameSchulzeHandler() {
        this.htmlText = new StringBuilder("<html><h1>Warnungen Schulze-Verfahren</h1>");
        this.hasMod = false;
    }
    
    @Override
    public void addWarningText(String text) {
        htmlText.append(text);
        appendNewLine();
        hasMod = true;
    }
    
    @Override
    public void voterNotFound(String voter, int lineNum) {
        htmlText.append("Name \"");
        htmlText.append(voter);
        htmlText.append("\" konnte wurde nicht gefunden, Eintrag wird ignoriert. In Zeile ");
        htmlText.append(lineNum);
        appendNewLine();
        hasMod = true;
    }
    
    @Override
    public void handle() {
        if (hasMod) {
            final String text = htmlText.toString() + "</html>";
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InfoFrame(text, "Warnungen Schulze-Verfahren").setVisible(true);
            }
        });
        }
    }
    
    private void appendNewLine() {
        htmlText.append("<br/>");
    }
}
